package mysql

import (
	"fmt"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(50);unique;comment：账户;"`
	Password string `gorm:"type:char(32);comment:密码;"`
	Sale     string `gorm:"type:char(5);comment:盐;"`
	Mobile   string `gorm:"type:char(15);comment:手机号;"`
	Nickname string `gorm:"type:varchar(30);comment:昵称;"`
	Status   int8   `gorm:"type:tinyint(1);comment:状态 0正常 1 冻结;"`
	Avatar   string `gorm:"type:varchar(255);comment:头像;"`
}

// 账号密码登录
func (u *User) Login(username, password string) error {
	return DB.Where("username = ?", username).Where(
		"password = ?", password).First(&u).Error
}

// 创建用户
func (u *User) Reg() error {
	return DB.Create(&u).Error
}

// 查询用户
func (u *User) Log() error {
	return DB.Where("username = ?", u.Username).First(&u).Error
}

// 手机号查询用户
func (u *User) MobileLog(mobile string) error {
	return DB.Where("mobile = ?", mobile).First(&u).Error
}

// 手机号登录
func (u *User) TelLog() error {
	err = DB.Where("tel = ?", u.Mobile).First(&u).Error
	fmt.Println("查询到的数据........", u)
	return err
}

// 手机号查询
func (u *User) GetUserInfo(mobile string) error {
	return DB.Where("mobile = ?", mobile).First(&u).Error
}

// 账户查询
func (u *User) GetUsernameInfo(username string) error {
	return DB.Where("username = ?", username).First(&u).Error
}

// 手机号修改密码
func (u *User) UpdateByMobilePwd(id int64, pwd, sale string) error {

	return DB.Model(&u).Where("id = ?", id).Updates(User{Password: pwd, Sale: sale}).Error
}

// 修改头像
func (u *User) UpdateAvatar(id int64, avatar string) error {
	return DB.Model(&u).Where("id = ?", id).Update("avatar", avatar).Error
}
